<?php
include("VerifySession.php");
?>

<?php
include("Header.php");
include("SNManager.class.php");
include("Pagination.php");
$Pagination = new Pagination();
$db = new SNDataManager();

?>
<?php
    $result_level =$db->GetAllLevel(true);
    $usr_code=null;
    $user_id=null;
    $user_name=null;
    $user_l_name=null;
    $user_log_name=null;
    $user_log_pass = null;
    $user_level=null;
    //$isSetUID =false;
    if($_REQUEST["UID"])
    {  
       //$isSetUID = true;
       $user_id = $_REQUEST["UID"];
       if($user_id==0)
       {
            $usr_code = '';
            $user_name ='';
            $user_l_name ='';
            $user_log_name= '';
            $user_log_pass ='';
            $user_level = 1;
       }else
       {
        $usr_info= $db->FindUserInfo($user_id);
           while ($row = mysql_fetch_array($usr_info, MYSQL_ASSOC))
           {
                $usr_code = $row['CODE'];
                $user_name = $row['FISRT_NAME'];
                $user_l_name = $row['LAST_NAME'];
                $user_log_name= $row['LOGIN_NAME'];
                $user_log_pass = $row['LOG_PASS'];
                $user_level =  $row['LEVEL_ID'];
           }
       }
       unset($_REQUEST["UID"]);
    }
  //  echo $isSetUID;
?>

<?php

    $limit = $config['perpage'];
    $totalrows = mysql_fetch_array($db->GetUserRowCount());
    
    if(isset($_REQUEST['page']) && is_numeric(trim($_REQUEST['page']))){$page = mysql_real_escape_string($_REQUEST['page']);}else{$page = 1;}
    $startrow = $Pagination->getStartRow($page,$limit);

    //create page links
    if($config['showpagenumbers'] == true)
    {
    	$pagination_links = $Pagination->showPageNumbers($totalrows['total'],$page,$limit);
    }else
    {
        $pagination_links=null;
    }
    
    if($config['showprevnext'] == true)
    {
    	$prev_link = $Pagination->showPrev($totalrows['total'],$page,$limit);
    	$next_link = $Pagination->showNext($totalrows['total'],$page,$limit);
    }else
    {
        $prev_link=null;$next_link=null;
    }
    

    if(!isset($_REQUEST['orderby']) OR trim($_REQUEST['orderby']) == "")
    {
        $result =$db->GetTopUser(1);
    	$array = mysql_fetch_assoc($result);
    	//first field
    	$i = 0;
    	foreach($array as $key=>$value)
        {
    		if($i > 0){break;}else
            {
    		  $orderby=$key;
            }
    		$i++;		
    	}
        $sort="ASC";    
    }else
    {
      $orderby=mysql_real_escape_string($_REQUEST['orderby']);
    }
    
    //IF SORT NOT SET OR VALID, SET DEFAULT
    if(!isset($_REQUEST['sort']) OR ($_REQUEST['sort'] != "ASC" AND $_REQUEST['sort'] != "DESC"))
    {
    	//default sort
    		$sort="ASC";
    	}else
        {	
    		$sort= mysql_real_escape_string($_REQUEST['sort']);
    }
if(!$orderby) $orderby="CODE";
$result_account =$db->getAllUserPaging(true,$orderby,$sort, $startrow,$limit);
?>
<script>
function checkForm(frm)
{
	if (frm.txtUserCode.value == "" || frm.txtLevel.value=="" || frm.txtFirstName.value == ""|| frm.txtLastName.value == "" || frm.txtPasswords.value == "")
	{
         alert("Please complete all of the form");
         return false;
     }else if( frm.txtPasswords.value != frm.txtRePasswords.value)
     {
     	    alert("Passwords and Re type passwords don't miss match");
         	return false;
     }
}
</script>
            <!-- Article -->
            <div id="adbox">
            <h2><span>List Account</span></h2>
<div class="CSSTable" >
    <table width="1024" border="1" align="center">
  <!--</a><tr>
    <td align="center">Code</td>
    <td align="center">First Name</td>
    <td align="center">Last Name</td>
    <td align="center">Login Name</td>
    <td align="center">Level</td>
    <td align="center">Edit</td>
    <td align="center">Delete</td>
  </tr>-->
   <?php
   
   $array = mysql_fetch_assoc($result_account);
    foreach ($array as $key=>$value) 
    {
        if($key=='CODE' || $key=='FISRT_NAME' || $key=='LAST_NAME'  || $key=='LOGIN_NAME' || $key=='LEV_NAME'  ){
            
   
    	if($config['nicefields'])
        {
        	$field = str_replace("_"," ",$key);
        	$field = ucwords($field);
    	}
    	
    	$field = $db->columnSortArrows($key,$field,$orderby,$sort);
    	echo "<th>" . $field . "</th>\n";
        }
    }
    echo "<th>" ?><a href="AccountPage.php?UID=0">New Account</a><?php "</th>\n";
    echo "<th>&nbsp;</th>\n";
    echo "</tr>\n";

//reset result pointer
mysql_data_seek($result_account,0);

         while ($row = mysql_fetch_array($result_account, MYSQL_ASSOC))
         { ?>
  <tr>
    <td><?php echo $row['CODE']; ?></td>
    <td><?php echo $row['FISRT_NAME']; ?></td>
    <td><?php echo $row['LAST_NAME']; ?></td>
    <td><?php echo $row['LOGIN_NAME']; ?></td>
    <td><?php echo $row['LEV_NAME']; ?></td>
    <td align="center"><a href="AccountPage.php?UID=<?php echo $row['ID']; ?>">EDIT</a></td>
    <td align="center"><a href="DelAccount.php?UID=<?php echo $row['ID']; ?>" onclick="return confirm('Are you sure you want to delete this?')"  >DELETE</a></td>
  </tr>
    <?php }  ?>
    
    <tr>
    <th colspan="7" align="center" >
                <?php
            if(!($prev_link==null && $next_link==null && $pagination_links==null)){
            echo '<div class="pagination">';
            echo $prev_link;
            echo $pagination_links;
            echo $next_link;
            echo '<div style="clear:both;"></div>';
            echo "</div>";
            }
            ?>
    </th>
    </tr>
</table>
</div>
</div> <!-- /article -->

<?php if($user_id >= 0){ ?>
<div id="adbox" align="center">
<h2><span>Account Form</span></h2>
<form id="frmAccount" name="frmAccount" method="post" action="SaveUser.php" onsubmit="return checkForm(this)">
<table width="800" border="0" align="center">
  <tr>
    <td align="left">Code</td>
    <td><label for="txtUserCode"></label> <input type="text" name="txtUserCode" id="txtUserCode" value="<?php echo $usr_code; ?>" /></td>
    <td align="left">Level</td>
    <td><label for="txtLevel">
      <select name="txtLevel" id="txtLevel">
        <?php
         while ($row = mysql_fetch_array($result_level, MYSQL_ASSOC))
         { ?>
        <option id ="<?php echo "LEVEL".$row['ID']; ?>"
                value="<?php echo $row['ID']; ?>" <?php if($user_level == $row['ID']) echo"selected"; ?>
         ><?php echo $row['CODE'].' '.$row['LEV_NAME']; ?></option>
        <?php }  ?>
      </select>
    </label></td>
  </tr>
  <tr>
    <td align="left">First Name</td>
    <td><input type="text" name="txtFirstName" id="txtFirstName" value="<?php echo $user_name; ?>"  /></td>
    <td align="left">Last Name</td>
    <td><input type="text" name="txtLastName" id="txtLastName" value="<?php echo $user_l_name; ?>"  /></td>
  </tr>
  <tr>
    <td align="left">Login Name</td>
    <td><input type="text" name="txtLoginName" id="txtLoginName" value="<?php echo $user_log_name; ?>"  /></td>
    <td align="left">&nbsp;</td>
    <td><input type="hidden" name="hdUserID" id="hdUserID" value="<?php echo $user_id; ?>"  /></td>
  </tr>
  <tr>
    <td align="left">Passwords</td>
    <td><input type="password" name="txtPasswords" id="txtPasswords"  value="<?php echo $user_log_pass; ?>" /></td>
    <td align="left">Retype Passwords</td>
    <td><input type="password" name="txtRePasswords" id="txtRePasswords" value="<?php echo $user_log_pass; ?>" /></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td align="right">&nbsp;</td>
    <td align="left">&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td align="right">&nbsp;</td>
    <td align="left">
    <input type="submit" value="Save" /> &nbsp;&nbsp;
    <input type="reset" value="Cancel" onclick="location.href = 'AccountPage.php';" />
    </td>
    <td>&nbsp;</td>
  </tr>
</table>
</form>
</div> <!-- /article -->
<?php } ?>            
  <?php
  include("Footer.php");
  ?>
